﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Web;
using Entidades.Sistema.Produccion;
using Mapping.Sistema.Produccion;
using Entidades.Sistema.Distribucion;
using Mapping.Sistema.Distribucion;


namespace Modelo.Sistema.Distribucion
{
    public class CatalogoVehiculo
    {
        private List<Vehiculo> Col_Vehiculo;

        public CatalogoVehiculo()
        {
            List<Vehiculo> vehuiculos = (List<Vehiculo>)HttpContext.Current.Cache["Col_Vehiculo"];
            if (vehuiculos == null)
            {
                vehuiculos = MappingVehiculo.ConsultarVehiculos();
                HttpContext.Current.Cache["Col_Vehiculo"] = vehuiculos;
            }
            Col_Vehiculo = vehuiculos;
        }

        public ReadOnlyCollection<Vehiculo> ListarVehiculo()
        {
            return Col_Vehiculo.AsReadOnly();
        }

        public bool AgregarVehiculo(Vehiculo oVehiculo)
        {
            oVehiculo.IdVehiculo = GetMayor();
            oVehiculo.IdVehiculo = oVehiculo.IdVehiculo + 1;
            Col_Vehiculo.Add(oVehiculo);
            Mapping.Sistema.Distribucion.MappingVehiculo.AgregarVehiculo(oVehiculo);
            HttpContext.Current.Cache["Col_Vehiculo"] = Col_Vehiculo;
            return true;
        }

        public bool EliminarVehiculo(Vehiculo oVehiculo)
        {
            Col_Vehiculo.Remove(oVehiculo);
            MappingVehiculo.EliminarVehiculo(oVehiculo);
            HttpContext.Current.Cache["Col_Vehiculo"] = Col_Vehiculo;
            return true;
        }
        public void ModificarVehiculo(Vehiculo oVehiculo)
        {
            MappingVehiculo.ModificarVehiculo(oVehiculo);
            Col_Vehiculo.RemoveAll(x => x.IdVehiculo == oVehiculo.IdVehiculo);
            Col_Vehiculo.Add(oVehiculo);
            HttpContext.Current.Cache["Col_Vehiculo"] = Col_Vehiculo;
        }
        public Vehiculo BuscarVehiculo(int IdVehiculo)
        {
            return this.Col_Vehiculo.Find(vehi => vehi.IdVehiculo == IdVehiculo);
        }
        public bool ValidarExistencia(Vehiculo oVehiculo)
        {
            if (this.Col_Vehiculo.Count(vehi => vehi.IdVehiculo == oVehiculo.IdVehiculo) == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public Int32 GetMayor()
        {
            if (Col_Vehiculo.Count == 0) return 0;

            Int32 maxIdVehiculo =
                (from veh in Col_Vehiculo
                 select veh.IdVehiculo)
                .Max();

            return maxIdVehiculo ;
        }
    }
}
